package com.nokia.framework.util.web;

import lombok.Data;
import lombok.NoArgsConstructor;
import net.sf.json.JSON;
import net.sf.json.JSONObject;

/**
 * 操作是否成功的JSON对象表示。
 */
@Data
@NoArgsConstructor
public class ResultData<T> {

	public final static String STAT_FAILURE = "0";
	public final static String STAT_SUCCESS = "1";

	/** 操作结果：0.失败；1.成功 */
	private String stat;

	// 给客户端的提示信息
	private String message;

	private T data;

	public ResultData(String stat, String message) {
		this.stat = stat;
		this.message = message;
	}

	public static ResultData success(String message) {
		ResultData result = new ResultData(STAT_SUCCESS, message);
		result.setData("");
		return result;
	}

	public static ResultData success(String message, Object data) {
		ResultData result = new ResultData(STAT_SUCCESS, message);
		result.setData(data == null ? "" : data);
		return result;
	}

	public static JSON successToJson(String message) {
		ResultData result = new ResultData(STAT_SUCCESS, message);
		result.setData("");
		return result.toJson();
	}

	public static JSON failureToJson(String message) {
		ResultData result = new ResultData(STAT_FAILURE, message);
		result.setData("");
		return result.toJson();
	}

	public static ResultData failure(String message) {
		ResultData result = new ResultData(STAT_FAILURE, message);
		result.setData("");
		return result;
	}

	public JSON toJson() {
		return JSONObject.fromObject(this);
	}
}
